async function sendQuery() {
        const selected = Array.from(document.querySelectorAll('input[name="knowledge_bases"]:checked'))
            .map(cb => cb.value);
        const question = document.getElementById("question").value;

        if (!question.trim()) return;

        // 添加用户消息
        const userMessage = document.createElement("div");
        userMessage.className = "message user";
        const userContent = document.createElement("div");
        userContent.className = "message-content";
        userContent.innerText = question;
        userMessage.appendChild(userContent);
        document.getElementById("chat-box").appendChild(userMessage);

        // 添加系统回复区域
        const botMessage = document.createElement("div");
        botMessage.className = "message bot";
        const botContent = document.createElement("div");
        botContent.className = "message-content";
        botMessage.appendChild(botContent);
        const imageContainer = document.createElement("div");
        imageContainer.className = "image-container";
        botMessage.appendChild(imageContainer);
        document.getElementById("chat-box").appendChild(botMessage);

        // 自动滚动到底部
        document.getElementById("chat-box").scrollTop = document.getElementById("chat-box").scrollHeight;

        // 清空输入框
        document.getElementById("question").value = "";

        // 发送请求
        const reader = await fetch("/chat/rag/query", {
            method: "POST",
            headers: {"Content-Type": "application/json"},
            body: JSON.stringify({knowledge_bases: selected, question: question})
        }).then(res => res.body.getReader());

        const decoder = new TextDecoder();
        let buffer = "";
        let images_rendered = false;

        while (true) {
            const { done, value } = await reader.read();
            if (done) break;

            buffer += decoder.decode(value, { stream: true });
            const lines = buffer.split("\n");
            buffer = lines.pop();

            for (const line of lines) {
                if (line.trim() === "") continue;
                try {
                    const data = JSON.parse(line);
                    if (data.images && !images_rendered) {
                        data.images.forEach(img => {
                            const imgElement = document.createElement("img");
                            imgElement.src = img;
                            imageContainer.appendChild(imgElement);
                        });
                        images_rendered = true;
                    } else {
                        botContent.innerText += data.response || "";
                    }
                } catch (e) {
                    console.error("解析失败：", line);
                }
            }
        }

        // 再次滚动到底部（确保图片加载后）
        document.getElementById("chat-box").scrollTop = document.getElementById("chat-box").scrollHeight;
    }